package edu.sctu.zcy.month201506.day20150619;


import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by user24 on 2015/6/23.
 */
public class HttpPost2 {


    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

    public void sendPost(){

        HttpPost httpPost = new HttpPost("http://localhost:8090/departmentweb/manage");

        List<NameValuePair> paramsList = new ArrayList<>();
        paramsList.add(new BasicNameValuePair("username","admin"));
        paramsList.add(new BasicNameValuePair("password","admin"));

        httpPost.setEntity(new UrlEncodedFormEntity(paramsList,Consts.UTF_8));

        try {
            HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();

            if(entity != null){
                System.out.println("回复内容："+entity.getContent());
                System.out.println("回复内容.......："+ EntityUtils.toString(entity));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
